<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/11/13
// +----------------------------------------------------------------------

namespace app\common\services;

use app\common\facade\Arr;
use app\common\facade\TransItem;
use think\Db;

/**
 *
 * Class JobService
 * @package app\common\services
 * @author feraner<feraner@163.com>
 * @date 2019/11/13
 */
class JobService
{


    /**
     * 获取兼职详细信息
     * @param       $id
     * @param array $need | 取值：content,userApply,publishUser
     *
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function jobDetail($id, $need = [])
    {
        $result = Db::name('jobs')->alias('j')
            ->where('j.id', $id)
            ->join('jobs_type jt', 'jt.id = j.work_type')
            ->field('j.*,jt.name as jobTypeName,jt.img as jobTypeImg')
            ->find() ?: [];
        
        //兼职内容表
        if (in_array('content', $need)) {
            $result['extendContent'] = Db::name('jobs_content')
                ->where('job_id', Arr::get($result, 'id', 0))
                ->find() ?: [];
            if ($result['extendContent']) {
                $result['extendContent']['contentArr'] = array_filter(explode(PHP_EOL, $result['extendContent']['content']));
                $result['extendContent']['requirementArr'] = array_filter(explode(PHP_EOL, $result['extendContent']['requirement']));
                $result['extendContent']['work_welfareArr'] = array_filter(explode(PHP_EOL, $result['extendContent']['work_welfare']));
            }
        }
        //兼职报名表
        if (in_array('userApply', $need)) {
            $result['extendApplyList'] = Db::name('jobs_apply')->alias('ja')
                ->where('ja.job_id', Arr::get($result, 'id'))
                ->join('user u', 'u.id = ja.user_id')
                ->field('ja.*, u.name as userName, u.avatar as userAvatar')
                ->select() ?: [];
        }
        //发布兼职的人
        if (in_array('publishUser', $need) && Arr::get($result, 'user_id')) {
            $result['extendPublishUser'] = Db::name('user')->alias('u')
                ->where('u.id', Arr::get($result, 'user_id'))
                ->join('user_extend ue', 'u.id = ue.user_id')
                ->field('u.id,u.name,u.truename,u.avatar,u.type,ue.publish_job_num')
                ->find() ?: [];
        }

        $result['salary_unitCh'] = TransItem::transMoneyUnit(Arr::get($result, 'salary_unit'));
        $result['settlement_typeCh'] = TransItem::transSettlement(Arr::get($result, 'settlement_type'));
        $result['genderCh'] = TransItem::transGender(Arr::get($result, 'gender'));
        $result['statusCh'] = TransItem::transJobPassStatus(Arr::get($result, 'status'));
        $result['work_timeCh'] = date('Y年m月d日', Arr::get($result, 'work_time'));
        $result['work_time_endCh'] = date('Y年m月d日', Arr::get($result, 'work_time_end'));
        return $result;
    }


    /**
     * 兼职列表
     * @param       $where
     * @param       $page
     * @param       $pageSize
     * @param array $need
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function jobList($where, $page, $pageSize, $need = [])
    {
        $jobDb = Db::name('jobs')->alias('j')
            ->where('j.is_deleted', 0);
        //状态
        if(Arr::get($where, 'status', -1) != -1) {
            $jobDb->where('j.status', Arr::get($where, 'status'));
        } else {
            if(Arr::get($where, 'is_center')){
                $jobDb->where('j.status', 'IN', [1,2, 4, 5]);
            }else{
                $jobDb->where('j.status', 'IN', [2, 4, 5]);
            }
        }

        //性别
        if ($gender = Arr::get($where, 'gender')) {
            $jobDb->where('j.gender', $gender);
        }
        //结算方式
        if ($settlementType = Arr::get($where, 'settlementValue')) {
            $jobDb->where('j.settlement_type', $settlementType);
        }
        //兼职类型
        if ($jobType = Arr::get($where, 'jobType')) {
            $jobDb->where('j.work_type', $jobType);
        }
        //tag
        if ($tagId = Arr::get($where, 'tagId')) {
            $jobDb->where('j.tag', 'LIKE', '%' . $tagId . '%');
        }
        //地区
        if ($provincesId = Arr::get($where, 'provincesId')) {
            $jobDb->where('j.provinces_id', $provincesId);
        }
        if ($cityId = Arr::get($where, 'cityId')) {
            $jobDb->where('j.city_id', $cityId);
        }
        if ($countyId = Arr::get($where, 'countyId')) {
            $jobDb->where('j.county_id', $countyId);
        }

        //发布人
        if ($publishId = Arr::get($where, 'publishUserId')) {
            $jobDb->where('j.user_id', $publishId);
        }

        //根据jobId来获取
        if ($jobIdArr = Arr::get($where, 'jobIdArr')) {
            $jobDb->where('j.id', 'IN', $jobIdArr);
        }
        
        //根据 兼职标题 获取
        if ($searchTitle = Arr::get($where, 'searchTitle', '')) {
            $jobDb->where('j.title', 'LIKE', '%' . $searchTitle . '%');
        }

        //置顶
        if ($isTop = Arr::get($where, 'isTop', 0)) {
            $jobDb->where('j.is_top', 1);
        }
        
        //推荐
        if ($isRecommend = Arr::get($where, 'is_recommend', 0)) {
            $jobDb->where('j.is_recommend', $isRecommend);
        }

        //依据商家
        if (Arr::has($where, 'busId')) {
            if (Arr::get($where, 'busId')) {
                $jobDb->where('j.business_id', Arr::get($where, 'busId'));
            } else {
                $jobDb->where('j.business_id', '<>', 0);
            }
        }

        $jobList = $jobDb->join('jobs_type jt', 'jt.id = j.work_type')
            ->field('jt.name as jobTypeName, jt.img as jobTypeImg, jt.job_num as jobTypeNum')
            ->field('j.*')
            ->order('is_top DESC,is_recommend DESC,updated_at DESC')
            ->page($page, $pageSize)
            ->select() ?: [];

        foreach ($jobList as &$item) {
            $item['work_timeCh'] = date('m月d日', Arr::get($item, 'work_time')) . '-' .
                date('m月d日', Arr::get($item, 'work_time_end'));
            $item['settlement_typeCh'] = TransItem::transSettlement(Arr::get($item, 'settlement_type'));
            $item['salary_unitCh'] = TransItem::transMoneyUnit(Arr::get($item, 'salary_unit'));
            $item['statusCh'] = TransItem::transJobPassStatus(Arr::get($item, 'status'));
            $item['isOver'] = (Arr::get($item, 'work_time_end') < time()) ? true : false;
        }

        return $jobList;
    }



}
